﻿using LiWi;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HYTool
{
    public class Data
    {
        /// <summary>
        /// 当前运行配方
        /// </summary>
        public int runRecipe = 0;
        /// <summary>
        /// 是否开启通讯：1：开启，0：关闭
        /// </summary>
        public int openSocket = 0;

        public IntPtr handle=new IntPtr();
        /// <summary>
        /// IP地址
        /// </summary>
        public string ip="192.168.1.11";
        /// <summary>
        /// 取料位
        /// </summary>
        public float iniPos = 0;
        /// <summary>
        /// 运动点数
        /// </summary>
        public int posNum = 0;
        /// <summary>
        /// 运动点位
        /// </summary>
        public float[] posList=new float[] { 0 };
        /// <summary>
        /// 数据保存路劲
        /// </summary>
        public string file = AppDomain.CurrentDomain.BaseDirectory + "Data.ini";

        /// <summary>
        /// 保存INI文件
        /// </summary>
        /// <param name="section">配方名</param>
        public void SaveIni(string section)
        {
            try
            {
                //保存配方
                //先删除原有节点
                IniAPI.INIDeleteSection(file, section);
                IniAPI.INIWriteValue(file, section, "是否开启通讯", openSocket.ToString());
                IniAPI.INIWriteValue(file, section, "取料位", iniPos.ToString());
                IniAPI.INIWriteValue(file, section, "点数", (posList.Count()).ToString());
                for (int i = 0; i < posList.Count(); i++)
                {
                    IniAPI.INIWriteValue(file, section, $"运动点{i}", posList[i].ToString());
                }
            }
            catch(AggregateException e)
            {
                MessageBox.Show(e.ToString());
            }
            
        }
        /// <summary>
        /// 加载配方
        /// </summary>
        /// <param name="section"></param>
        public void LoadIni(string section)
        {
            try
            {
                openSocket = System.Convert.ToInt32(IniAPI.INIGetStringValue(file, section, "是否开启通讯", "0"));
                iniPos = System.Convert.ToSingle(IniAPI.INIGetStringValue(file, section, "取料位", "0"));
                posNum = System.Convert.ToInt16(IniAPI.INIGetStringValue(file, section, "点数", "0"));
                posList = new float[System.Convert.ToInt16(posNum)];
                for (int i = 0; i < System.Convert.ToInt16(posNum); i++)
                {
                    posList[i] = System.Convert.ToSingle(IniAPI.INIGetStringValue(file, section, $"运动点{i}", "0"));
                }
            }
            catch(AggregateException e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
}
